package com.example.campus_activity_backend.controller;

import com.example.campus_activity_backend.common.BaseResponse;
import com.example.campus_activity_backend.common.ErrorCode;
import com.example.campus_activity_backend.common.ResultUtil;
import com.example.campus_activity_backend.domain.SysUser;
import com.example.campus_activity_backend.exception.BusinessException;
import com.example.campus_activity_backend.service.SysRoleService;
import com.example.campus_activity_backend.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private SysUserService userService;

    @PostMapping("add")
    public BaseResponse<Long> addUser(@RequestBody SysUser user){
        //TODO 验证用户身份
        long userId = userService.addUser(user);
        return ResultUtil.success(userId);
    }

    @GetMapping("/{id}")
    public BaseResponse<SysUser> searchUserById(@PathVariable Integer id){
        //校验参数
        if(id == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"id为空");
        }
        SysUser user = userService.getById(id);
        //用户信息脱敏
        SysUser safetyUser = userService.getSafetyUser(user);
        return ResultUtil.success(safetyUser);
    }

    @GetMapping("search/{loginName}")
    public BaseResponse<SysUser> searchUserByLoginName(@PathVariable String loginName){
        //校验参数
        if(loginName == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"查询用户名为空");
        }
        //用户信息脱敏
        SysUser safetyUser = userService.getUserByLoginName(loginName);
        return ResultUtil.success(safetyUser);
    }
}
